package com.chisheng.caa.service;

import com.chisheng.caa.pojo.DuplicateStrategy;
import com.chisheng.caa.pojo.ImportCourseSetting;
import com.chisheng.caa.pojo.dto.GradeDTO;
import com.chisheng.caa.pojo.dto.ImportCourseSettingDTO;
import com.chisheng.caa.util.BeanUtil;
import lombok.Getter;
import lombok.ToString;
import com.chisheng.caa.pojo.dto.CourseDTO;
import org.springframework.lang.Nullable;

import java.util.Objects;

public abstract class AbstractImportCourseService extends AbstractCRUD<CourseDTO> implements ImportCourseService {

    public AbstractImportCourseService(CacheUtil cacheUtil) {
        super(cacheUtil);
    }

    @Override
    public DuplicateStrategy getDuplicateStrategy(@Nullable String accessKey) {
        ImportCourseState state = cacheUtil.checkAndGet(accessKey);
        return Objects.requireNonNull(state.getSetting().getDuplicateStrategy());
    }

    @Override
    public ImportCourseSetting getSetting(String accessKey) {
        ImportCourseState state = cacheUtil.checkAndGet(accessKey);
        return state.getSetting();
    }

    @Override
    public void setSetting(String accessKey, ImportCourseSetting setting) {
        ImportCourseState state = cacheUtil.checkAndGet(accessKey);
        state.setSetting(setting);
    }

    @Getter
    @ToString
    public static class ImportCourseState extends AbstractState<CourseDTO> {

        protected ImportCourseSetting setting;

        public void setSetting(ImportCourseSetting setting) {
            if (this.setting == null) {
                this.setting = new ImportCourseSettingDTO(setting);
            } else {
                BeanUtil.copyProperties(setting, this.setting, true);
            }
        }

    }

}
